﻿#include "lix/lix.h"

#include "lix/text.h"
#include "lix/xml/document.h"
#include "lix/xml/node.h"
#include "lix/xml/parser.h"



namespace lix{	
	namespace xml{


//==============================================================================
// XmlDocument
//==============================================================================

// コンストラクタ
XmlDocument::XmlDocument()
	:Node( kNodeRoot )
{
	m_Result = kParseNone;
	m_Allocator = new NodeAllocator();
}

// デストラクタ
XmlDocument::~XmlDocument()
{
	if( m_Allocator ){
		delete m_Allocator;
	}
}


ParseResult XmlDocument::parse( const std::string& text, uint32_t options  )
{
	if( text.empty()){
		m_Result = xml::kParseEmptyText;
		return m_Result;
	}
	m_Text.assign( text.begin(), text.end() );
	m_Text.push_back(0);
	
	m_Result = parseImpl(options);
	return m_Result;
}
	

// パース
ParseResult XmlDocument::parse( const char* pch,  uint32_t options )
{
	if( ! pch || *pch == 0 ){
		m_Result = xml::kParseEmptyText;
		return m_Result;
	}
	size_t length = strlen(pch);
	m_Text.assign( pch, pch + ( length + 1 ));
	
	m_Result = parseImpl(options);
	return m_Result;
}

lix::xml::ParseResult XmlDocument::parse( lix::CharSequence sequence, uint32_t options /*= 0 */ )
{
	return parse( sequence.str(), options );
}

lix::xml::ParseResult XmlDocument::parseImpl(uint32_t options)
{
	if( ! m_Allocator->create()){
		return xml::kParseAllocationFailed;
	}

	Parser parser( this, m_Allocator); 

	return parser.parse( & m_Text[0], options);
}

const char* XmlDocument::getSourceString() const
{
	if( m_Text.empty()){
		return NULL;
	}
	return &m_Text[0];
}

IDocumentLocalAllocator* XmlDocument::getLocalAllocator()
{
	return m_Allocator;
}










} //namespace xml





} // namespace  lix



